package com.demo.mvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo.aop.domain.Model;
import com.demo.test.HttpTest;

//@SessionAttributes(value = { "model" }, types = { Model.class })
//用于requestMapping，更精确请求
//headers = { "Accept-Language=en-US,zh;q=0.8" } 
//params = { "username","age!=10" }
//@RestController

@Controller
@RequestMapping("/mvc")
public class RestController {

	@RequestMapping("/http")
	public String http() {
		HttpTest.send();
		return "index";
	}
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(String name, String phone, HttpServletRequest request) {
		return "index";

	}
	// rest风格
	@RequestMapping(value = "/register", method = RequestMethod.PUT)
	public String register(@ModelAttribute("model") Model model, Map<String, Object> map) {
		System.err.println("model:" + model);
		map.put("model", model);
		// "redirect:/login";
		return "success";
	}

	// 所有requet方法执行前都会执行此方法，添加注解：@ModelAttribute
	@ModelAttribute
	public void prepareGetDomain(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
		System.err.println("modelAttribute method");
		if (id == null) {
			// 模拟从数据库中获取对象
			Model model = new Model(1);
			map.put("model", model);
		}
	}

}
